package com.timeDeal.service;



import com.timeDeal.bean.JubaoBean;
import com.timeDeal.util.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 叼爸爸 on 2016/9/6.
 */
public class JubaoService {

    public List<JubaoBean> getAllJubao(int cu){
        Connection cn = DButil.getConnection();
        String mysql="select  *   from  jubao   where  shifouchuli=?  limit  "+(cu*3)+",3" ;
        PreparedStatement ps=null;
        ResultSet rs=null;
        List<JubaoBean>  xfb = new ArrayList<>() ;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setString(1,"否");
            rs = ps.executeQuery();
            while (rs.next()){
                JubaoBean xb=new JubaoBean();
                xb.setJubaoid(rs.getInt("jubaoid"));
                xb.setJubaoyuanyin(rs.getString("jubaoyuanyin"));
                xb.setJubaorenid(rs.getInt("jubaorenid"));
                xb.setJubaoduixiangid(rs.getInt("jubaoduixiangid"));
                xfb.add(xb);
            }
            return xfb ;
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null ;
    }
    public   String  getCount(){
        Connection cn = DButil.getConnection();
        String mysql="select count( *)   from  jubao   where shifouchuli=?  "   ;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setString(1,"否");
            rs = ps.executeQuery();
            while (rs.next()){
                return   rs.getString(1);
            }
            return  "";
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  "" ;
    }

    /**
     * 举报处理更新
     * @param id   举报id
     * @return  成功与否
     */
    public  boolean JubaoChuli(int id )  {
        Connection conn = DButil.getConnection() ;
        String mysql="update jubao set shifouchuli=? where jubaoid=?";
        PreparedStatement ps=null;
        try{
            ps=conn.prepareStatement(mysql);
            ps.setString(1,"是");
            ps.setInt(2,id);
            return   ps.execute() ;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  false;
    }

    /**
     * 根据id得到一个jubaobean
     * @param id  举报id
     * @return  Jubaobean
     */
    public JubaoBean  getAJubaoById(int id){
        Connection cn = DButil.getConnection();
        String mysql="select  jubaoyuanyin  from  jubao where jubaoid=?"  ;
        PreparedStatement ps=null;
        JubaoBean jubaoBean = new JubaoBean();
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
                if(rs.next())
                    jubaoBean.setJubaoyuanyin(rs.getString(1));
                return  jubaoBean ;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  null;
    }

    public  boolean addAjubao(JubaoBean jb ){
        Connection cn = DButil.getConnection();
        String mysql="insert into  jubao (jubaoid ,jubaorenid,jubaoduixiangid,jubaoriqi,jubaoyuanyin,shifouchuli )"
                +" values(?,?,?,?,?,?)";
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            ps=cn.prepareStatement(mysql);
            ps.setInt(1,DButil.getZhuJianMax("jubaoid","jubao"));
            ps.setInt(2,jb.getJubaorenid());
            ps.setInt(3,jb.getJubaoduixiangid());
            ps.setDate(4,jb.getJubaoriqi());
            ps.setString(5,jb.getJubaoyuanyin());
            ps.setString(6,jb.getShifouchuli());
            ps.execute();
            return true ;
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if(ps!=null){
                    ps.close();
                }
                if(rs!=null){
                    rs.close();
                }
                cn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return  false ;
    }
}
